package mongo.db.clients.firsts;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

public class CBSFromFile {

	List<ServerAddress> seeds = null;
	MongoClient client = null;

	DB db = null;
	static DBCollection collection = null;

	public CBSFromFile() {
		seeds = new ArrayList<ServerAddress>();
		try {
			seeds.add(new ServerAddress("202.191.136.234", 27017));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		client = new MongoClient(seeds);

		db = client.getDB("dopinventory");
		collection = db.getCollection("devices");
	}

	public static void main(String[] args) {
		
		CBSFromFile up = new CBSFromFile();
		
		try {
			BufferedReader br = new BufferedReader(new FileReader("/tmp/data.xlsx"));
			String line;
			while ((line = br.readLine()) != null) {
				// process the line.
				DBObject find = new BasicDBObject("_id", line);
				DBCursor result = collection.find(find);
				
				DBObject update = new BasicDBObject("$set" , new BasicDBObject("config.cbs", "YES"));
				collection.update(find, update, true, false);
				System.out.println( " ip : " + line + " - cbs : " + result.next().get("config"));
 //				System.out.println(result.get("_id" + " : " + result.get("config.cbs")));
				
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.exit(1);
		

	}

}
